using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_CategoryAdmin : System.Web.UI.Page
{
    CategoryDAO daoCategory = new CategoryDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) LoadData();
    }
    protected void dgrCategories_RowEditing(object sender, GridViewEditEventArgs e)
    {
        dgrCategories.EditIndex = e.NewEditIndex;
        LoadData();
    }
    public void LoadData()
    {

        dgrCategories.DataSource = daoCategory.GetAll();
        dgrCategories.DataBind();
    }
    protected void dgrCategories_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int cate_id = int.Parse(dgrCategories.DataKeys[e.RowIndex].Value.ToString());
        BookCategory cate = new BookCategory();
        cate.CategoryID = cate_id;
        TextBox txtName = (TextBox)dgrCategories.Rows[e.RowIndex].FindControl("txtName");
        cate.CategoryName = txtName.Text;
        if (daoCategory.Update(cate) > 0)
        {
            dgrCategories.EditIndex = -1;
            LoadData();
        }
        
    }
    protected void dgrCategories_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgrCategories.EditIndex = -1;
    }
    protected void dgrCategories_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int cate_id = int.Parse(dgrCategories.DataKeys[e.RowIndex].Value.ToString());
        BookCategory cate = new BookCategory();
        cate.CategoryID = cate_id;
        if (daoCategory.Delete(cate) > 0)
        {
            LoadData();
        }
        dgrCategories.EditIndex = -1;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        BookCategory cate = new BookCategory();
        cate.CategoryName = txtCategoryname.Text;
        if (daoCategory.Insert(cate) > 0)
        {
            dgrCategories.EditIndex = -1;
            LoadData();
        }
    }
}
